// ==========================================================================
// GFast自动生成logic操作代码。
// 生成日期：2024-01-04 11:04:57
// 生成路径: internal/app/flow/logic/flow_form.go
// 生成人：gfast
// desc:流程表单
// company:云南奇讯科技有限公司
// ==========================================================================

package logic

import (
	"context"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/tiger1103/gfast/v3/internal/app/flow/dao"
	"github.com/tiger1103/gfast/v3/internal/app/flow/model"
	"github.com/tiger1103/gfast/v3/internal/app/flow/model/do"
	"github.com/tiger1103/gfast/v3/internal/app/flow/service"
	"github.com/tiger1103/gfast/v3/internal/app/system/consts"
	systemService "github.com/tiger1103/gfast/v3/internal/app/system/service"
	"github.com/tiger1103/gfast/v3/library/liberr"
)

func init() {
	service.RegisterFlowForm(New())
}

func New() service.IFlowForm {
	return &sFlowForm{}
}

type sFlowForm struct{}

func (s *sFlowForm) List(ctx context.Context, req *model.FlowFormSearchReq) (listRes *model.FlowFormSearchRes, err error) {
	listRes = new(model.FlowFormSearchRes)
	err = g.Try(ctx, func(ctx context.Context) {
		m := dao.FlowForm.Ctx(ctx).WithAll()
		if req.Name != "" {
			m = m.Where(dao.FlowForm.Columns().Name+" like ?", "%"+req.Name+"%")
		}
		if req.Status != "" {
			m = m.Where(dao.FlowForm.Columns().Status+" = ?", gconv.Int(req.Status))
		}
		if len(req.DateRange) != 0 {
			m = m.Where(dao.FlowForm.Columns().CreatedAt+" >=? AND "+dao.FlowForm.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
		}
		listRes.Total, err = m.Count()
		liberr.ErrIsNil(ctx, err, "获取总行数失败")
		if req.PageNum == 0 {
			req.PageNum = 1
		}
		listRes.CurrentPage = req.PageNum
		if req.PageSize == 0 {
			req.PageSize = consts.PageSize
		}
		order := "id asc"
		if req.OrderBy != "" {
			order = req.OrderBy
		}
		var res []*model.FlowFormListRes
		err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取数据失败")
		listRes.List = make([]*model.FlowFormListRes, len(res))
		for k, v := range res {
			listRes.List[k] = &model.FlowFormListRes{
				Id:          v.Id,
				Name:        v.Name,
				Status:      v.Status,
				Remark:      v.Remark,
				CreatedAt:   v.CreatedAt,
				CreatedUser: v.CreatedUser,
				CreatedBy:   v.CreatedBy,
			}
		}
	})
	return
}

func (s *sFlowForm) GetById(ctx context.Context, id uint64) (res *model.FlowFormInfoRes, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		err = dao.FlowForm.Ctx(ctx).WithAll().Where(dao.FlowForm.Columns().Id, id).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取信息失败")
	})
	return
}

func (s *sFlowForm) Add(ctx context.Context, req *model.FlowFormAddReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.FlowForm.Ctx(ctx).Insert(do.FlowForm{
			Name:      req.Name,
			Status:    req.Status,
			Remark:    req.Remark,
			Conf:      req.Conf,
			Fields:    req.Fields,
			CreatedBy: systemService.Context().GetUserId(ctx),
		})
		liberr.ErrIsNil(ctx, err, "添加失败")
	})
	return
}

func (s *sFlowForm) Edit(ctx context.Context, req *model.FlowFormEditReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.FlowForm.Ctx(ctx).WherePri(req.Id).Update(do.FlowForm{
			Name:      req.Name,
			Status:    req.Status,
			Remark:    req.Remark,
			Conf:      req.Conf,
			Fields:    req.Fields,
			UpdatedBy: systemService.Context().GetUserId(ctx),
		})
		liberr.ErrIsNil(ctx, err, "修改失败")
	})
	return
}

func (s *sFlowForm) Delete(ctx context.Context, ids []uint64) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.FlowForm.Ctx(ctx).Delete(dao.FlowForm.Columns().Id+" in (?)", ids)
		liberr.ErrIsNil(ctx, err, "删除失败")
	})
	return
}
